package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.util.List;
import java.util.Map;

import com.tycmc.functions.util.MapGetter;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
import net.tycmc.bulb.dbutil.global.GlobalDBVar;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSDAO;
import net.tycmc.cty.ctyms.timejob.iems.service.ChangeInstallRecordService;

public class ChangeInstallRecordDao {
	private IBaseDAO baseDAO;
	private ITransactionDAO tranDAO;
	public ITransactionDAO getTranDAO() {
		return tranDAO;
	}

	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}

	/**
	 * @return the baseDAO
	 */
	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	/**
	 * @param baseDAO the baseDAO to set
	 */
	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	/**
	 * 获取统计日期内发动机序列号变更的记录
	 * @param msgtime
	 * @return
	 */
	public List<Map<String,Object>> getAllChangeRecord (String msgtime){
		String table=GlobalDBVar.getDBFullName_Conn("Msg_Track_"+msgtime.replaceAll("-","").substring(0,6), "3", null);
		String sql="select vehicleid,MsgTk_ProductSerialNo esnno,convert(varchar(20),MsgTk_MsgTime,120) msgtime from v_inteloilnew inner join "+table+" on vehicleid=MsgTk_Vcl_ID where substring(convert(varchar(20),msgtk_msgtime,120),1,10)='"+msgtime+"' and MsgTk_ProductSign=1 order by msgtk_msgtime";
		List<Map<String,Object>>list=baseDAO.execQuery(sql, new Object[]{});
		return list;
	}
	/**
	 * 获取统计日期内发动机序列号变更时上报的现行故障
	 * @param msgtime
	 * @return
	 */
	public List<Map<String,Object>> getAllHappeningFltInChangeInstall (Map<String,Object>map){
		String statetime=MapGetter.getString(map,"statetime");
		String bgnTime=MapGetter.getString(map,"msgtime");
		String endTime=MapGetter.getString(map,"endmsgtime");
		String vclid=MapGetter.getString(map,"vehicleid");
		String flttable=GlobalDBVar.getDBFullName_Conn("Msg_Flt_Content_"+statetime.replaceAll("-","").substring(0,8), "4",statetime);
		String fltdre=GlobalDBVar.getDBFullName_Conn("Sys_FltISM", "1",null);
		String sql="select vehicleid,SysISM_Code from v_inteloilnew inner join "+flttable+" on vehicleid=MsgFCo_Vcl_ID and MsgFCo_Vcl_ID="+vclid+" inner join "+fltdre+" on MsgFCo_FaultSPN=SysISM_SPN and MsgFCo_FaultFMI=SysISM_FMI " +
				"where MsgFCo_MsgTime>='"+bgnTime+"' and MsgFCo_MsgTime<='"+endTime+"' and MsgFCo_FltType=0";
		List<Map<String,Object>>list=baseDAO.execQuery(sql, new Object[]{});
		return list;
	}
	/**
	 * 获取统计日期内发动机序列号变更时上报的非现行故障
	 * @param msgtime
	 * @return
	 */
	public List<Map<String,Object>> getAllNo_HappeningFltInChangeInstall (Map<String,Object>map){
		String bgnTime=MapGetter.getString(map,"msgtime");
		String endTime=MapGetter.getString(map,"endmsgtime");
		String vclid=MapGetter.getString(map,"vehicleid");
		String flttable=GlobalDBVar.getDBFullName_Conn("Msg_Flt_Reply_DM2", "3",null);
		String fltdre=GlobalDBVar.getDBFullName_Conn("Sys_FltISM", "1",null);
		String sql="select vehicleid,SysISM_Code from v_inteloilnew inner join "+flttable+" on vehicleid=MsgFRDM_Vcl_ID and MsgFRDM_Vcl_ID="+vclid+" inner join "+fltdre+" on MsgFRDM_FltSPN=SysISM_SPN and MsgFRDM_FltFMI=SysISM_FMI " +
				"where MsgFRDM_MsgTime>='"+bgnTime+"' and MsgFRDM_MsgTime<='"+endTime+"' ";
		List<Map<String,Object>>list=baseDAO.execQuery(sql, new Object[]{});
		return list;
	}
	/**
	 * 获取统计日期内发动机序列号变更时上报的非现行故障
	 * @param msgtime
	 * @return
	 */
	public Map<String,Object> getLastESN (String vehicleid){
		String sql="select top 1 Esncr_changeLatEsn from ESNChangeRecord where Esncr_vcl_id="+vehicleid+" order by Esncr_changeTime desc";
		Map<String,Object>map=baseDAO.execQueryOne(sql, new Object[]{});
		return map;
	}
	
	/**
	 * 保存换装记录
	 * @param map
	 * @return
	 */
	public  void saveInstallRecord(Map<String,Object>map){
		//设备id
		String vehicleid=MapGetter.getString(map,"vehicleid");
		//统计日期
		String statetime=MapGetter.getString(map,"statetime");
		//换装前esn
		String preesn=MapGetter.getString(map,"preesn");
		//换装后esn
		String esnno=MapGetter.getString(map,"esnno");
		//现行故障
		String happingflt=MapGetter.getString(map,"happingflt");
		//非现行故障
		String no_happingflt=MapGetter.getString(map,"no_happingflt");
		//换装时间
		String msgtime=MapGetter.getString(map,"msgtime");
		String sql="insert into ESNChangeRecord(Esncr_vcl_id,Esncr_changeTime,Esncr_changePreEsn,Esncr_changeLatEsn," +
				"Esncr_happenIngFltCode,Esncr_NoHappenIngFltCode,Esncr_MsgTime)values("+vehicleid+",'"+msgtime+"','"+preesn+"','"+esnno+"','"+happingflt+"','"+no_happingflt+"','"+statetime+"')";
		ChangeInstallRecordService.listsql.add(sql);
	}
	public int execInstallRecordSql(List<String>listsql){
		MyConnectDSDAO md=new MyConnectDSDAO();
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<listsql.size();i++){
			sb.append(listsql.get(i));
			if(i==listsql.size()-1){//最后一条直接执行
				md.execInsert(sb.toString(),new Object[]{});
				sb=new StringBuffer();
			}else{
				if(i%10==9){
					md.execInsert(sb.toString(),new Object[]{});
					sb=new StringBuffer();
				}
			}
		}
		return 1;
	}
}
